-
Notifications
You must be signed in to change notification settings - Fork 4
/
prog6.c
102 lines (93 loc) · 2.36 KB
/
prog6.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Sparse Matrix using Doubly linked list
#include<stdio.h>
#include<stdlib.h>
struct node
{
int row, col, data;
struct node *next;
struct node *prev;
};
typedef struct node *NODE;
NODE insertend(NODE start, int row, int col, int data){
NODE temp, current;
temp = (NODE)malloc(sizeof(struct node));
temp->row = row;
temp->col = col;
temp->data = data;
temp->prev = NULL;
temp->next = NULL;
if (start == NULL){
return temp;
}
current = start;
while(current->next != NULL){
current = current->next;
}
current->next = temp;
temp->prev = current;
return start;
}
void dispalay(NODE start){
NODE temp;
if(start == NULL){
printf("List is Empty\n");
}
else{
printf("\nRow\tCol\tData\n");
temp = start;
while(temp != NULL){
printf("%d\t%d\t%d\t\n",temp->row, temp->col, temp->data );
temp = temp->next;
}
}
}
void displaymatrix(NODE start, int row, int col){
int i, j;
NODE temp;
temp = start;
printf("The Matrix is:\n");
for(i=1; i<=row; i++){
for(j=1; j<=col; j++){
if(temp!=NULL && temp->row == i && temp->col == j){
printf("%d\t", temp->data );
temp = temp->next;
}
else{
printf("0\t");
}
}
printf("\n");
}
}
int main()
{
NODE start = NULL;
int m, n, i, j, item, temp, row, col;
printf("\nEnter the order of Sparse Matrix\n");
scanf("%d%d", &m,&n);
// printf("\nRead Matrix\n");
printf("\nEnter the number of non-zero entries:");
scanf("%d", &temp);
printf("\nNOTE: Please take first index as 1\n");
for(i=1; i<=temp; i++){
printf("\nEnter the row number of [%d] element: ", i);
scanf("%d", &row);
printf("\nEnter the col number of [%d] element: ", i);
scanf("%d", &col);
printf("\nEnter the [%d] element:", i);
scanf("%d", &item);
start = insertend(start, row, col, item);
}
// for(i=1; i<=m; i++){
// for(j=1; j<=n; j++){
// scanf("%d", &item);
// if(item != 0){
// start = insertend(start, i, j, item);
// }
// }
// }
printf("The linked list is:\n");
dispalay(start);
displaymatrix(start, m, n);
return 0;
}